package com.app.lxx.pregnancyberth.service;

import android.app.Service;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.app.lxx.pregnancyberth.widge.MyToast;

import java.util.List;

import top.wuhaojie.bthelper.BtHelperClient;
import top.wuhaojie.bthelper.OnSearchDeviceListener;

public class BleService extends Service {
    private static final String TAG = "BleService";
    private BtHelperClient btHelperClient;
    Handler handler = new Handler();

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent != null) {
            boolean startSearch = intent.getBooleanExtra("startSearch", false);
            if (startSearch) {
                searchDevices();
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initBle();
    }

    private void searchDevices() {
        btHelperClient.searchDevices(new OnSearchDeviceListener() {

            @Override
            public void onStartDiscovery() {
                // 在进行搜索前回调
                Log.d(TAG, "onStartDiscovery()");
            }

            @Override
            public void onNewDeviceFound(BluetoothDevice device) {
                // 当寻找到一个新设备时回调
                Log.d(TAG, "new device: " + device.getName() + " " + device.getAddress());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
//                        Toast.makeText(BleService.this, "发现蓝牙: " + device.getName() + " " + device.getAddress(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onSearchCompleted(List<BluetoothDevice> bondedList, List<BluetoothDevice> newList) {
                // 当搜索蓝牙设备完成后回调
                Log.d(TAG, "SearchCompleted: bondedList" + bondedList.toString());
                Log.d(TAG, "SearchCompleted: newList" + newList.toString());
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(TAG, "searchDevices next");
                        searchDevices();
                    }
                }, 2000);
            }

            @Override
            public void onError(Exception e) {
                e.printStackTrace();
                Log.d(TAG, "SearchonError: " + e.getMessage());
            }
        });
    }

    private void initBle() {
        btHelperClient = BtHelperClient.from(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        btHelperClient.close();
    }
}
